/*
 * @Description: Mock适配器配置 - 购物网站Mock数据
 * 记得注释
 */
import { uniappMockResponse, uniappRequestAdapter } from '@alova/adapter-uniapp'
import { createAlovaMockAdapter } from '@alova/mock'
import {
  mockCategories,
  mockBanners,
  mockProducts,
  mockUser,
  mockAddresses,
  mockOrders,
  mockReviews,
  mockCoupons,
  generateRandomProduct,
  generateRandomOrder,
} from './modules/mall'
import type { 
  Product, 
  PageResult,
  ApiResponse 
} from '@/types/mall'

// 模拟延迟
const delay = (ms: number = 300) => new Promise(resolve => setTimeout(resolve, ms))

// 创建成功响应
const createSuccessResponse = <T>(data: T): ApiResponse<T> => ({
  code: 200,
  message: 'success',
  data,
  success: true,
})

// 创建分页响应
const createPageResponse = <T>(list: T[], page: number = 1, pageSize: number = 20): ApiResponse<PageResult<T>> => {
  const start = (page - 1) * pageSize
  const end = start + pageSize
  const pageList = list.slice(start, end)
  
  return createSuccessResponse({
    list: pageList,
    total: list.length,
    page,
    pageSize,
    hasMore: end < list.length,
  })
}

// 购物网站Mock定义
const mallMocks = [
  // 商品相关API
  {
    url: '/api/products',
    method: 'GET',
    response: ({ query }: any) => {
      const { page = 1, pageSize = 20, categoryId } = query
      let products = [...mockProducts]
      
      // 按分类筛选
      if (categoryId) {
        products = products.filter(p => p.categoryId === categoryId)
      }
      
      // 如果商品不够，生成随机商品
      while (products.length < 50) {
        products.push(generateRandomProduct(categoryId as string))
      }
      
      return createPageResponse(products, Number(page), Number(pageSize))
    }
  },
  
  {
    url: '/api/products/:productId',
    method: 'GET',
    response: ({ params }: any) => {
      const product = mockProducts.find(p => p.id === params.productId) || mockProducts[0]
      return createSuccessResponse(product)
    }
  },
  
  // 分类相关API
  {
    url: '/api/categories',
    method: 'GET',
    response: () => {
      return createSuccessResponse(mockCategories)
    }
  },
  
  // 轮播图API
  {
    url: '/api/banners',
    method: 'GET',
    response: () => {
      return createSuccessResponse(mockBanners)
    }
  },
  
  // 用户相关API
  {
    url: '/api/user/login',
    method: 'POST',
    response: ({ data }: any) => {
      return createSuccessResponse({
        user: mockUser,
        token: 'mock_token_' + Date.now(),
        refreshToken: 'mock_refresh_token_' + Date.now(),
      })
    }
  },
  
  {
    url: '/api/user/info',
    method: 'GET',
    response: () => {
      return createSuccessResponse(mockUser)
    }
  },
  
  // 订单相关API
  {
    url: '/api/orders',
    method: 'GET',
    response: ({ query }: any) => {
      const { status, page = 1, pageSize = 20 } = query
      let orders = [...mockOrders]
      
      // 状态筛选
      if (status) {
        orders = orders.filter(o => o.status === status)
      }
      
      // 生成更多订单
      while (orders.length < 10) {
        orders.push(generateRandomOrder(mockUser.id))
      }
      
      return createPageResponse(orders, Number(page), Number(pageSize))
    }
  },
  
  // 购物车API
  {
    url: '/api/cart',
    method: 'GET',
    response: () => {
      return createSuccessResponse([])
    }
  },
  
  {
    url: '/api/cart',
    method: 'POST',
    response: ({ data }: any) => {
      return createSuccessResponse({ message: '添加成功' })
    }
  },
]

// 创建mock适配器
const mockAdapter = createAlovaMockAdapter(mallMocks, {
  // 使用uniapp请求适配器处理非mock请求
  httpAdapter: uniappRequestAdapter,

  // 使用uniapp mock响应适配器
  onMockResponse: uniappMockResponse,

  // 根据环境变量启用/禁用mock
  enable: true,

  // 添加延迟以模拟网络延迟 (200-600ms)
  delay: Math.random() * 400 + 200,

  // 在开发环境下打印mock请求日志
  mockRequestLogger: import.meta.env.MODE === 'development',
  // 路径匹配模式 - 使用完整路径匹配
  matchMode: 'pathname',
})

export default mockAdapter
